<?php

namespace app\admin\controller;

use app\common\model\Users as UsersModel;

class Users extends Common
{
    //会员列表
    public function index()
    {
        if (request()->isPost()) {
            $key = input('post.key');
            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('pageSize');
            $list = db('users')->alias('u')
                ->where('u.mobile|u.username', 'like', "%" . $key . "%")
                ->order('u.id desc')
                ->paginate(['list_rows' => $pageSize, 'page' => $page])
                ->toArray();
            foreach ($list['data'] as $k => $v) {
                $list['data'][$k]['reg_time'] = date('Y-m-d H:s', $v['reg_time']);
            }
            return ['code' => 0, 'msg' => '获取成功!', 'data' => $list['data'], 'count' => $list['total']];
        }
        return $this->fetch();
    }
    
    //设置会员状态
    public function usersState()
    {
        $id = input('post.id');
        $is_lock = input('post.is_lock');
        if (db('users')->where('id=' . $id)->update(['is_lock' => $is_lock]) !== false) {
            return ['status' => 1, 'msg' => '设置成功!'];
        } else {
            return ['status' => 0, 'msg' => '设置失败!'];
        }
    }
    
    public function userEdit()
    {
        if (request()->isPost()) {
            $user = db('users');
            $data = input('post.');
            if (empty($data['password'])) {
                unset($data['password']);
            } else {
                $data['password'] = md5($data['password']);
            }
            if (isset_full($data, 'id')) {
                $rs = $user->update($data);
            } else {
                $rs = $user->insert($data);
            }
            
            if ($rs) {
                $result['msg'] = '操作成功!';
                $result['url'] = url('index');
                $result['code'] = 1;
            } else {
                $result['msg'] = '会员修改失败!';
                $result['code'] = 0;
            }
            return $result;
        } else {
            $id = input('id');
            $info = UsersModel::get($id);
            $this->assign('info', $info);
            $this->assign('title', lang('edit') . lang('user'));
            return $this->fetch();
        }
    }
    
    public function getRegion()
    {
        $Region = db("area");
        $pid = input("pid");
        $list = $Region->where(['pid' => $pid])->select();
        return $list;
    }
    
    public function usersDel()
    {
        db('users')->delete(['id' => input('id')]);
        db('oauth')->delete(['uid' => input('id')]);
        return ['code' => 1, 'msg' => '删除成功!'];
    }
    
    public function delAll()
    {
        $map[] = ['id', 'IN', input('param.ids/a')];
        db('users')->where($map)->delete();
        $result['msg'] = '删除成功！';
        $result['code'] = 1;
        $result['url'] = url('index');
        return $result;
    }
    
    /** -- 会员组 -- */
    //列表
    public function groupList()
    {
        if (request()->isPost()) {
            $list = db('usergroup')->order('sort')->select();
            return ['code' => 0, 'msg' => '获取成功!', 'data' => $list];
        }
        return $this->fetch();
    }
    
    //编辑
    public function groupEdit()
    {
        if (request()->isPost()) {
            $data = input('post.');
            if ($data['id'] > 0) {
                db('usergroup')->update($data);
            } else {
                unset($data['id']);
                db('usergroup')->save($data);
            }
            $result['msg'] = '操作成功!';
            $result['url'] = url('groupList');
            $result['code'] = 1;
            return $result;
        } else {
            $map['id'] = input('param.id');
            $info = db('usergroup')->where($map)->find();
            $this->assign('title', lang('edit') . "会员组");
            $this->assign('info', $info);
            return $this->fetch('users_user_group_edit');
        }
    }
    
    //删除
    public function groupDel()
    {
        $id = input('id');
        if (empty($id)) {
            return ['code' => 0, 'msg' => '会员组ID不存在！'];
        }
        db('usergroup')->where(['id' => $id])->delete();
        return ['code' => 1, 'msg' => '删除成功！'];
    }
    
    //排序
    public function groupOrder()
    {
        $userLevel = db('usergroup');
        $data = input('post.');
        $userLevel->update($data);
        $result['msg'] = '排序更新成功!';
        $result['url'] = url('userGroup');
        $result['code'] = 1;
        return $result;
    }
    
}